package com.nayun.gjh.xiaokabanghou.controller;

import com.nayun.gjh.xiaokabanghou.common.*;
import com.nayun.gjh.xiaokabanghou.model.User;
import com.nayun.gjh.xiaokabanghou.model.request.LoginRequest;
import com.nayun.gjh.xiaokabanghou.model.request.SafetyUpdateUserRequest;
import com.nayun.gjh.xiaokabanghou.model.request.SafetyUserRequest;
import com.nayun.gjh.xiaokabanghou.model.response.SafetyLoginResponse;
import com.nayun.gjh.xiaokabanghou.model.response.SafetyUserResponse;
import com.nayun.gjh.xiaokabanghou.service.UserService;
import io.swagger.annotations.Tag;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = {"http://localhost:5173/"}, allowCredentials = "true")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 账户登录状态请求API
     * @param httpServletRequest
     * @return safetyUser
     */
    @GetMapping("/current")
    public BaseResponse<SafetyLoginResponse> getCurrentUser(HttpServletRequest httpServletRequest) {
        SafetyUserRequest loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        // todo 校验用户合法性
        SafetyLoginResponse safetyUser = userService.getSafetyUser(loginUser);
        if (safetyUser.getUc() == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        return ResultUtils.success(safetyUser);

    }


    @GetMapping("/send-code")
    public BaseResponse<String> sendMail(HttpServletRequest httpServletRequest) throws Exception {
        SafetyUserRequest loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        String stuNumMail = userService.sendCode(loginUser);
        if (stuNumMail == null) {
            return ResultUtils.error(ErrorCode.DATA_NULL, "用户未填写学号");
        }
        return ResultUtils.success(stuNumMail);

    }

    @GetMapping("/safety-check")
    public BaseResponse<String> userSafetyCheck(String sixCode,HttpServletRequest httpServletRequest) throws Exception {
        SafetyUserRequest loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        String userId = userService.userSafetyCheck(sixCode,loginUser,httpServletRequest);
        if (userId == null) {
            return ResultUtils.error(ErrorCode.DATA_NULL, "验证码错误");
        }
        return ResultUtils.success(userId);
    }

    @GetMapping("/safety-current")
    public BaseResponse<SafetyLoginResponse> getSafetyCurrentUser(HttpServletRequest httpServletRequest) {
        SafetyUserRequest safetyStatusUser = HttpSessionUtil.getSafetyUser(httpServletRequest);
        if (safetyStatusUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        // todo 校验用户合法性
        SafetyLoginResponse safetyUser = userService.getSafetyUser(safetyStatusUser);
        if (safetyUser.getUc() == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        return ResultUtils.success(safetyUser);

    }

    @GetMapping("/card")
    public BaseResponse<SafetyUserResponse> userCord(HttpServletRequest httpServletRequest) throws Exception {
        SafetyUserRequest loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        SafetyUserResponse safetyUserResponse = userService.cardInfor(loginUser);
        return ResultUtils.success(safetyUserResponse);
    }

    /**
     * 手机号登录
     * @param loginRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/phone")
    public BaseResponse<String> userLogin(@RequestBody LoginRequest loginRequest, HttpServletRequest httpServletRequest) throws Exception {
        String phone = loginRequest.getPhone();
        if (phone == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        String userId = userService.userLogin(phone, httpServletRequest);
        if (userId == null) {
            throw new BusinessException(ErrorCode.DATA_NULL, "返回数据为空");
        }
        return ResultUtils.success(userId);
    }

    @PostMapping("/update")
    public BaseResponse<String> userCordUpdate(@RequestBody SafetyUpdateUserRequest safetyUpdateUserRequest, HttpServletRequest httpServletRequest) throws Exception {
        SafetyUserRequest loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        Boolean result = userService.updatecardInfor(safetyUpdateUserRequest, loginUser);
        if (!result) {
            throw new BusinessException(ErrorCode.DATA_NULL, "返回数据为空");
        }
        return ResultUtils.success("更新成功");
    }


    @GetMapping("/card/lost-status")
    public BaseResponse<String> updateMyCardStatusToLost(HttpServletRequest httpServletRequest) throws Exception {
        SafetyUserRequest loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        Boolean result = userService.cardStatusToRecord(loginUser);
        if (!result) {
            return ResultUtils.error(ErrorCode.DATA_NULL, "校园卡卡号或其他信息不完整");
        }
        return ResultUtils.success("更新成功");
    }

    @GetMapping("/card/find-status")
    public BaseResponse<String> updateMyCardStatusToFind(HttpServletRequest httpServletRequest) throws Exception {
        SafetyUserRequest loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        Boolean result = userService.cardStatusToFind(loginUser);
        if (!result) {
            return ResultUtils.error(ErrorCode.DATA_NULL, "校园卡卡号或其他信息不完整");
        }
        return ResultUtils.success("更新成功");
    }
}
